<?php

/**
 * 阿里oss对象存储api
 *
 * @author Whb
 */
require './Api/Ali/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
class Alapi {
    private  $ak;
    private  $as;
    private  $ossClient;
    private  $endpoint;

    public function __construct() { 
        $this->_ak = C('AL_AK');
        $this->_as = C('AL_AS');
        $this->_endpoint =C('ENDPOINT');
       $this->_ossClient = new OssClient($this->_ak,$this->_as,$this->_endpoint);
    }
/**
 * 列出用户所有的Bucket
 *
 * @param OssClient $ossClient OssClient实例
 * @return null
 */
    function listBuckets()
    { 
        $bucketList = null;
        try{
            $bucketListInfo =$this->_ossClient->listBuckets();
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        $bucketList = $bucketListInfo->getBucketList();
        foreach($bucketList as $bucket) {
            print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
        }
    }
    
    
/**
 * 上传指定的本地文件内容
 *
 * @param OssClient $ossClient OSSClient实例
 * @param string $bucket 存储空间名称
 * @return null
 */
function uploadFile($bucket,$object,$filePath)
{
    //$object = "oss-php-sdk-test/1.png";
    try{
        $res = $this->_ossClient->uploadFile($bucket, $object, $filePath);
    } catch(OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    return $res;
}
 
}
